#include <stdio.h>
#include "wrap.h"
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{
        if(argc < 2){printf("./a.out port\n");return -1;}
        int port = atoi(argv[1]);
        
        int fd = Socket(AF_INET,SOCK_STREAM,0);
        
        struct sockaddr_in addr;
        memset(&addr,0,sizeof(addr));
        addr.sin_family=AF_INET;
        addr.sin_port = htons(port);
        inet_pton(AF_INET,"127.0.0.1", &addr.sin_addr.s_addr);
        
        Connect(fd,(struct sockaddr*)&addr,sizeof(addr));
        
        char buf[1024] = {0};
        while (1){
                printf("请输入要发送的字符串:\n");
                fgets(buf, sizeof(buf), stdin);
                Write(fd, buf, strlen(buf));

                //等待接收数据
                memset(buf,0,sizeof(buf));
                int len = Read(fd, buf, sizeof(buf));
                if(len == -1){
                        perror("read error");
                        break;
                }else if(len == 0){
                        printf("服务器端关闭了连接\n");
                        break;
                }else{
                        printf("recv buf: %s \n", buf);
                }
        }
        Close(fd);
        return 0;
}
